package br.com.lazyprogrammer.spaceshooter.model;

import java.util.Random;

import br.com.lazyprogrammer.spaceshooter.model.listener.CollisionListener;
import br.com.lazyprogrammer.spaceshooter.model.meteor.BigBrown;
import br.com.lazyprogrammer.spaceshooter.model.meteor.BigGray;
import br.com.lazyprogrammer.spaceshooter.model.meteor.MediumBrown;
import br.com.lazyprogrammer.spaceshooter.model.meteor.MediumGray;
import br.com.lazyprogrammer.spaceshooter.model.meteor.SmallBrown;
import br.com.lazyprogrammer.spaceshooter.model.meteor.SmallGray;
import br.com.lazyprogrammer.spaceshooter.model.meteor.TinyBrown;
import br.com.lazyprogrammer.spaceshooter.model.meteor.TinyGray;
import br.com.lazyprogrammer.spaceshooter.model.renderer.WorldRenderer;
import br.com.limaogames.framework.model.DynamicGameObject;

/**
 * Created by Andre on 20/08/2014.
 */
public class Meteor extends DynamicGameObject implements CollisionListener {

    public static int healthBar;

    public static float speed;
    public static float stateTime = 0;

    public Meteor(float x, float y, float width, float height, int healthBar, float speed, float vx, float vy) {
        super(x, y, width, height);

        this.healthBar = healthBar;
        this.speed = speed;
        this.velocity.set(vx, vy);
    }

    public void update(float deltaTime) {
        position.add(velocity.x * deltaTime, velocity.y * deltaTime);
        bounds.lowerLeft.set(position).sub(bounds.width / 2, bounds.height / 2);

        if(position.x + bounds.width < 0)
            position.x = WorldRenderer.FRUSTUM_WIDTH + bounds.width;
        if(position.x - bounds.width > WorldRenderer.FRUSTUM_WIDTH)
            position.x = -bounds.width;

        if (position.y + bounds.height < 0)
            position.y = WorldRenderer.FRUSTUM_HEIGHT + bounds.height;
        if (position.y - bounds.height > WorldRenderer.FRUSTUM_HEIGHT)
            position.y = -bounds.height;

        stateTime += deltaTime;
    }

    public static Meteor create(Random rand, int type, float x, float y, float vx, float vy) {
        int subtype = 0;
        switch (type) {
            case 0:
                subtype = rand.nextInt(4);
                return new BigBrown(x, y, subtype, vx, vy);
            case 1:
                subtype = rand.nextInt(4);
                return new BigGray(x, y, subtype, vx, vy);
            case 2:
                subtype = rand.nextInt(2);
                return new MediumBrown(x, y, subtype, vx, vy);
            case 3:
                subtype = rand.nextInt(2);
                return new MediumGray(x, y, subtype, vx, vy);
            case 4:
                subtype = rand.nextInt(2);
                return new SmallBrown(x, y, subtype, vx, vy);
            case 5:
                subtype = rand.nextInt(2);
                return new SmallGray(x, y, subtype, vx, vy);
            case 6:
                subtype = rand.nextInt(2);
                return new TinyBrown(x, y, subtype, vx, vy);
            case 7:
                subtype = rand.nextInt(2);
                return new TinyGray(x, y, subtype, vx, vy);
        }
        return null;
    }

    public static float getHeight(int type) {
        switch (type) {
            case 0:
                return BigBrown.METEOR_HEIGHT;
            case 1:
                return BigGray.METEOR_HEIGHT;
            case 2:
                return MediumBrown.METEOR_HEIGHT;
            case 3:
                return MediumGray.METEOR_HEIGHT;
            case 4:
                return SmallBrown.METEOR_HEIGHT;
            case 5:
                return SmallGray.METEOR_HEIGHT;
            case 6:
                return TinyBrown.METEOR_HEIGHT;
            case 7:
                return TinyGray.METEOR_HEIGHT;
        }
        return 0;
    }

    public static float getSpeed(int type) {
        switch (type) {
            case 0:
                return BigBrown.DEFAULT_SPEED;
            case 1:
                return BigGray.DEFAULT_SPEED;
            case 2:
                return MediumBrown.DEFAULT_SPEED;
            case 3:
                return MediumGray.DEFAULT_SPEED;
            case 4:
                return SmallBrown.DEFAULT_SPEED;
            case 5:
                return SmallGray.DEFAULT_SPEED;
            case 6:
                return TinyBrown.DEFAULT_SPEED;
            case 7:
                return TinyGray.DEFAULT_SPEED;
        }
        return 0;
    }

    public static float getWidth(int type) {
        switch (type) {
            case 0:
                return BigBrown.METEOR_WIDTH;
            case 1:
                return BigGray.METEOR_WIDTH;
            case 2:
                return MediumBrown.METEOR_WIDTH;
            case 3:
                return MediumGray.METEOR_WIDTH;
            case 4:
                return SmallBrown.METEOR_WIDTH;
            case 5:
                return SmallGray.METEOR_WIDTH;
            case 6:
                return TinyBrown.METEOR_WIDTH;
            case 7:
                return TinyGray.METEOR_WIDTH;
        }
        return 0;
    }

    @Override
    public void hit(int damage) {
        healthBar -= damage;
    }

}
